//
//  FillOutViewController.m
//  jishiben
//
//  Created by 李钰 on 2017/12/7.
//  Copyright © 2017年 jishiben. All rights reserved.
//

#import "FillOutViewController.h"
#import "MyDbTools.h"
#import "YMPickerSelectView.h"
@interface FillOutViewController ()<UITextViewDelegate,YMPickerSelectViewDelegate>
@property (nonatomic,strong)UITextView * infoTextView;//内容
@property (nonatomic,strong)UIView * moodView;//心情
@property (nonatomic,strong)UIView * weatherView;//天气

@property (nonatomic,strong)UIButton * cancelBtn;
@property (nonatomic,strong)UIButton * submitBtn;
@property (nonatomic,strong)UILabel  * labelPlace;
@end

@implementation FillOutViewController
{
    NSInteger    _moodSelect;
    UIButton  *  _weatherSelect;
    UIButton  *  _moodBtn;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = RGB(243, 243, 247);
    [self.view addSubview: self.cancelBtn];
    [self.view addSubview: self.submitBtn];
    UIScrollView * scrollview = [[UIScrollView alloc] initWithFrame: CGRectMake(0, self.cancelBtn.bottom, ScreenWidth, ScreenHeight-(self.cancelBtn.bottom))];
    [self.view addSubview: scrollview];
    [scrollview addSubview: self.infoTextView];
    [scrollview addSubview: self.moodView];
    [scrollview addSubview: self.weatherView];
    scrollview.contentSize = CGSizeMake(0, self.weatherView.bottom+20);
    scrollview.userInteractionEnabled = YES;
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget: self action: @selector(onTap:)];
    [self.view addGestureRecognizer: tap];
    [self.infoTextView addSubview: self.labelPlace];
}
- (UITextView *)infoTextView
{
    if (nil == _infoTextView) {
        _infoTextView = [[UITextView alloc] initWithFrame: CGRectMake(15, 0, ScreenWidth-30, 250)];
        _infoTextView.layer.cornerRadius = 5.f;
        _infoTextView.layer.borderWidth = 0.5f;
        _infoTextView.layer.borderColor = RGBA(0, 0, 0, 0.2f).CGColor;
        _infoTextView.delegate = self;
        _infoTextView.font = [UIFont systemFontOfSize: 16];
    }
    return _infoTextView;
}
- (UILabel *)labelPlace
{
    if (nil == _labelPlace) {
        _labelPlace = [[UILabel alloc] initWithFrame: CGRectMake(10, 10, self.infoTextView.width-30, 20)];
        _labelPlace.text = STR(@"PAGETIPS");
        _labelPlace.font = [UIFont systemFontOfSize: 16];
        _labelPlace.textColor = RGBA(0, 0, 0, 0.6);
    }
    return _labelPlace;
}
- (UIButton *)cancelBtn
{
    if (nil == _cancelBtn) {
        _cancelBtn = [UIButton buttonWithType: UIButtonTypeCustom];
        [_cancelBtn setTitle: STR(@"Cancel") forState: UIControlStateNormal];
        [_cancelBtn setTitleColor: [UIColor blackColor] forState: UIControlStateNormal];
        _cancelBtn.frame = CGRectMake(0, UI_STATUS_BAR_HEIGHT, UI_NAVIGATION_BAR_HEIGHT+30, UI_NAVIGATION_BAR_HEIGHT);
        [_cancelBtn addTarget: self action: @selector(onCancel:) forControlEvents: UIControlEventTouchUpInside];
        _cancelBtn.titleLabel.font = [UIFont systemFontOfSize: 16];
    }
    return _cancelBtn;
}
- (UIButton *)submitBtn
{
    if (nil == _submitBtn) {
        _submitBtn = [UIButton buttonWithType: UIButtonTypeCustom];
        [_submitBtn setTitle: STR(@"Submit") forState: UIControlStateNormal];
        [_submitBtn setTitleColor: [UIColor blackColor] forState: UIControlStateNormal];
        _submitBtn.frame = CGRectMake(ScreenWidth-(UI_NAVIGATION_BAR_HEIGHT+30), UI_STATUS_BAR_HEIGHT, UI_NAVIGATION_BAR_HEIGHT+30, UI_NAVIGATION_BAR_HEIGHT);
        [_submitBtn addTarget: self action: @selector(onSubmit:) forControlEvents: UIControlEventTouchUpInside];
        _submitBtn.titleLabel.font = [UIFont systemFontOfSize: 16];
    }
    return _submitBtn;
}
- (UIView *)moodView
{
    if (nil == _moodView) {
        CGFloat height = 40.f;
        _moodView = [[UIView alloc] initWithFrame: CGRectMake(0, self.infoTextView.bottom, ScreenWidth, height+20)];
        
        ///
        UILabel * labelTitle = [[UILabel alloc] initWithFrame: CGRectMake(15, 20, ScreenWidth-30, 40)];
        labelTitle.font = [UIFont boldSystemFontOfSize: 16];
        labelTitle.textColor = [UIColor blackColor];
        labelTitle.text = [NSString stringWithFormat: @"%@：",STR(@"Mood")];
        [_moodView addSubview: labelTitle];
        
        _moodBtn = [UIButton buttonWithType: UIButtonTypeCustom];
        _moodBtn.frame = CGRectMake(70, 20, ScreenWidth-70-15, height);
        [_moodBtn addTarget: self action: @selector(onMood:) forControlEvents: UIControlEventTouchUpInside];
        _moodBtn.layer.cornerRadius = 5.f;
        [_moodBtn setTitle: STR(@"Commonly") forState: UIControlStateNormal];
        [_moodBtn setTitleColor: [UIColor whiteColor] forState: UIControlStateNormal];
        [_moodBtn setBackgroundColor: RGB(93, 182, 227)];
        [_moodView addSubview: _moodBtn];
        
        _moodSelect = 2;
    }
    return _moodView;
}
- (UIView *)weatherView
{
    if (nil == _weatherView) {
        CGFloat height = 30;
        CGFloat labelHeight = 28;
        _weatherView = [[UIView alloc] initWithFrame: CGRectMake(0, self.moodView.bottom, ScreenWidth, 40+4*(height+labelHeight)+40)];
        NSArray * name = [MyDbTools getWeatherNameList];
        NSArray * list = [MyDbTools getWeatherList];
        CGFloat space = 20;
        CGFloat width = (ScreenWidth-30-space*3)/4;
        
        ///
        UILabel * labelTitle = [[UILabel alloc] initWithFrame: CGRectMake(15, 0, ScreenWidth-30, 40)];
        labelTitle.font = [UIFont boldSystemFontOfSize: 16];
        labelTitle.textColor = [UIColor blackColor];
        labelTitle.text = [NSString stringWithFormat: @"%@：",STR(@"Weather")];
        [_weatherView addSubview: labelTitle];
        for (NSInteger i=0; i<list.count; i++) {
            NSString * str = list[i];
            UIButton * btn = [UIButton buttonWithType: UIButtonTypeCustom];
            [btn setImage: [UIImage imageNamed: str] forState: UIControlStateNormal];
//            btn.backgroundColor = [UIColor redColor];
            btn.tag = i;
            NSInteger j = i/4;
            btn.frame = CGRectMake(15+(width+space)*(i-j*4), (height+10+labelHeight)*j+labelTitle.height, width, height);
            [btn addTarget: self action: @selector(onWeather:) forControlEvents: UIControlEventTouchUpInside];
            UILabel * label = [[UILabel alloc] initWithFrame: CGRectMake(btn.left-10, btn.bottom, btn
                                                                         .width+20, labelHeight)];
            label.font = [UIFont systemFontOfSize: 11];
            label.textColor = [UIColor blackColor];
            label.text = name[i];
            label.textAlignment = NSTextAlignmentCenter;
            label.numberOfLines = 2;
            [_weatherView addSubview: label];
            [_weatherView addSubview: btn];
            
            if (i==0) {
                _weatherSelect = btn;
                btn.backgroundColor = RGBA(93, 182, 227, 0.6);
            }
        }
    }
    return _weatherView;
}
- (void)onTap:(UITapGestureRecognizer *)sender
{
    [self.view endEditing: YES];
}
- (void)onSubmit:(UIButton *)sender
{
    if (self.infoTextView.text.length == 0) {
        UIAlertView * alert = [[UIAlertView alloc] initWithTitle: STR(@"Kindly reminder") message: STR(@"nocontenttips") delegate: self cancelButtonTitle: STR(@"Sure") otherButtonTitles: nil];
        [alert show];
        return;
    }
    [self onTap: nil];
    [self dismissViewControllerAnimated: YES completion: nil];
    MoodObj * obj = [MoodObj new];
    obj.doText = self.infoTextView.text;
    NSInteger time = [[NSDate date] timeIntervalSince1970];
    NSString * dateStr = [NSString getDateTimeInterval: time Format: @"yyyy-MM-dd" isMS: NO];
    obj.creatDate = dateStr;
    obj.status = 1;
    obj.creatTime = time;
    obj.mood = _moodSelect;
    obj.weather = _weatherSelect.tag;
    if (self.block) {
        self.block(obj);
    }
}
- (void)onCancel:(UIButton *)sender
{
    [self onTap: nil];
    [self dismissViewControllerAnimated: YES completion: nil];
}
- (void)onMood:(UIButton *)sender
{
    NSArray * list = [MyDbTools getMoodTitleList];
    YMPickerSelectView * picker = [[YMPickerSelectView alloc] initWithDataParentSource: @[list] AndRowHeight: 52.f];
    picker.blackScreen = YES;
    picker.labelTitle.text = STR(@"Weather selection");
    [picker showCustomLineWithColor: RGB(66, 206, 205)];
    [picker setSelectIndexs: @[@(_moodSelect)]];
    picker.delegate = self;
    [picker show];
}
- (void)onWeather:(UIButton *)sender
{
    _weatherSelect.backgroundColor = [UIColor clearColor];
    sender.backgroundColor = RGBA(93, 182, 227, 0.6);
    _weatherSelect = sender;
}
-(void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - UITextViewDelegate
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    NSString * str = [NSString stringWithFormat: @"%@%@",textView.text,text];
    if (str.length >0) {
        self.labelPlace.hidden = YES;
    }else{
        self.labelPlace.hidden = NO;
    }
    return YES;
}
#pragma mark -YMPickerSelectViewDelegate
- (void)pickerViewSelectSource:(NSArray *)source
{
    _moodSelect = [[source firstObject] integerValue];
    NSArray * list = [MyDbTools getMoodTitleList];
    NSArray * moodColor = [MyDbTools getMoodColorList];
    [_moodBtn setTitle: list[_moodSelect] forState: UIControlStateNormal];
//    [_moodBtn setBackgroundColor: moodColor[_moodSelect]];
}
@end
